//
//  HZJCell.swift
//  WeiBo
//
//  Created by 陈贵 on 15/9/12.
//  Copyright © 2015年 hzj. All rights reserved.
//


import UIKit


/// 控件之间的间隙
let HZJMargin:CGFloat = 10
 /// 附件图片中的图片间隙
let HZJPICTUREMARGIN:CGFloat = 10
 /// 附加图片中小图片的宽
let HZJPICTUREWIDTH:CGFloat = 50
/// 附加图片中小图片的高
let HZJPICTUREHEIGHT:CGFloat = 50
/// 附加图片中每行小图片的个数
let HZJPICTURCOUNT:CGFloat = 3
///一张附加图片时的尺寸
let HZJPICTURESIZE:CGFloat = (HZJPICTUREWIDTH + HZJPICTUREMARGIN) * HZJPICTURCOUNT - HZJPICTUREMARGIN

class HZJCell: UITableViewCell {
    
    
    /// 据传入模型，计算cell的高度
    func calculateTheCellHeight(viewModel:StatusesViewModel) ->CGFloat{
        //获取模型数据
        statusesViewModel = viewModel
        //刷新约束
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)

    }
    
    /// 附加图片框的宽度约束
    var PictureWidth:NSLayoutConstraint?
    /// 附加图片框的高度约束
    var PictureHeight:NSLayoutConstraint?
    /// 顶部约束
    var pictureTop: NSLayoutConstraint?
    
    
    var statusesViewModel:StatusesViewModel? {
        didSet{
            topView.statusesViewModel = statusesViewModel
            
            textView.text = statusesViewModel?.statuses.text
            
            pictureView.statusesViewModel = statusesViewModel
            
            //计算附加图片框的约束
            PictureWidth?.constant = pictureView.bounds.width
            PictureHeight?.constant = pictureView.bounds.height
                        
            
            // 根据是否包含图片，决定顶部约束
            pictureTop?.constant = statusesViewModel?.thumbnailURLs?.count == 0 ? 0 : HZJPICTUREMARGIN
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUI()
//        setUI1()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
/// 老师的代码
     func setUI1(){
        // 顶部分割视图
        let topSepView = UIView()
        topSepView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        
        // 1. 添加控件
        contentView.addSubview(topSepView)
        contentView.addSubview(topView)
        contentView.addSubview(textView)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 2. 设置布局，cell 的默认大小是 320/44，iPhone 4时代的遗留问题，宽度不能直接使用cell的宽度
        let width = UIScreen.mainScreen().bounds.width
        // 1> 顶部分隔视图
        topSepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        
        // 2> 顶部视图
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSepView, size: CGSize(width: width, height: 35 + HZJPICTUREMARGIN))
        
        // 3> 正文标签
        textView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: HZJPICTUREMARGIN, y: HZJPICTUREMARGIN))
        
        // 4> 配图视图
        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: textView, size: CGSize(width: HZJPICTURESIZE, height: HZJPICTURESIZE), offset: CGPoint(x: 0, y: HZJPICTUREMARGIN))
        // 记录配图视图约束
        PictureWidth = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        PictureHeight = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        pictureTop = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)

        
        // 5> 底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -HZJPICTUREMARGIN, y: HZJPICTUREMARGIN))
    }
    
    
     func setUI(){
        let separatorView = UIView()
        separatorView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        contentView.addSubview(separatorView)
        contentView.addSubview(topView)
        contentView.addSubview(textView)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        
        //分割视图
        separatorView.frame.size = CGSizeMake(UIScreen.mainScreen().bounds.width,10)
        separatorView.frame.origin = CGPointZero
        
        //顶部视图
        topView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: separatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: separatorView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: separatorView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 60))
        

        //内容
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.backgroundColor = UIColor(white: 1, alpha: 1)
        addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: topView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: topView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: HZJMargin))
        
        //放于子类之中
//        //图片
//        pictureView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//       self.pictureTop = constraints.last
//        
//        addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 2 * HZJPICTURESIZE))
//        self.PictureWidth = constraints.last
//        
//        addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: HZJPICTURESIZE))
//        self.PictureHeight = constraints.last
        
        
        //底部视图
        bottomView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: pictureView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: HZJMargin))
        addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50))

    }
    
/// 顶部视图
    private lazy var topView:HZJTopOfCell = HZJTopOfCell()
///内容
     lazy var textView = UILabel(title: nil, color: UIColor.grayColor(), fontSize: 14,layoutWidth:UIScreen.mainScreen().bounds.width - 2 * HZJMargin)
 /// 图片
    lazy var pictureView:HZJPictureView = HZJPictureView()
///底部视图
     lazy var bottomView:HZJBottomOfCell = HZJBottomOfCell()
    
}
